;
(function() {
  window.approve = {
    init() {
      $('html, body').unbind()
      this.methods.ddwxTab()
      // 启用所有事件
      for (var event in this.event) {
        if (event != 'wxddNodeEvent') {
          this.event[event]()
        }
      }
    },
    data: {
      pid: null,
      type: null,
      initId: null,
      selectPerson: [],
      memberType: 'S',
      ddflag: false,
      getCount: false
    },
    methods: {
      //钉钉微信
      ddwxTab() {
        hlhttp.get({
          url: 'user_sub_organizations',
          data: {
            pid: 0,
            member_type: approve.data.memberType,
            show_member: 'N'
          },
          success: (data) => {
            if (!data.subOrgs.length) return;
            if (approve.data.ddflag) {
              data.subOrgs.map((item, index) => {
                if (data.subOrgs[index].orgType == 'WX') {
                  data.subOrgs.splice(index, 1)
                }
              })
            }
            approve.data.type = data.subOrgs[0].orgType;
            approve.data.initId = data.subOrgs[0].id;
            approve.dom.ddwxTabDom(data)
            approve.methods.detailList(data.subOrgs[0].id, $('.approve-bottom'))
          }
        })
      },
      //获取第一次拿到的实例数据
      detailList(pid, el, flag) {
        if (flag) {
          $('.approve-bottom').html('')
        }
        hlhttp.get({
          url: 'user_sub_organizations',
          data: {
            pid: pid,
            member_type: approve.data.memberType,
            show_member: 'N'
          },
          success: (data) => {
            if (!approve.data.getCount) {
              data.members = []
              approve.dom.wxddNodeList(data, el)
              approve.data.getCount = true
            } else {
              approve.methods.getMemberList(el[0].dataset.adminorgid, el[0].dataset.memberorgid, data, el)
            }

          }
        })
      },
      // 获取组织成员
      getMemberList(adminorgid, memberorgid, datalist, el) {
        hlhttp.get({
          url: 'organization/member',
          data: {
            org_head_id: adminorgid,
            org_member_id: memberorgid
          },
          success: (data) => {
            datalist.members = data
            approve.dom.wxddNodeList(datalist, el)
          }
        })
      }
      //
    },
    dom: {
      //添加节点,添加初始dom
      addinitialDom() {
        let len = $('.approve-person').length;
        let dom = `
          <div class="approve-top"></div>
          <div class="approve-bottom"></div>
        `;
        if (len) {
          $('.approve-person').append(dom)
        }
      },
      //钉钉微信导航dom
      ddwxTabDom(data) {
        let dom = '';
        data.subOrgs.map((item, index) => {
          let active = '';
          if (item.orgType == approve.data.type) {
            active = 'approve-tab-active'
          }
          dom += `
            <div class="approve-tab cursor ${ active }" data-index="${ index }" data-pid="${ item.pid }" data-id="${ item.id }" data-orgType="${ item.orgType }">${ item.shortName }</div>
          `
        })
        $('.approve-top').append(dom)
      },
      //微信钉钉列表dom
      wxddNodeList(data, el) {
        // let arr = data.subOrgs;
        let arr = [];
        data.members.map(item => {
          item.shortName = item.userName
        })
        // arr = data.members
        arr = arr.concat(data.subOrgs);
        let dom = '';
        arr.map((item, index) => {
          let icon = 'icon-Gm-prefix '
          if (item.userId) {
            icon = ''
          }
          dom += `
            <div class="approve-node" data-show="false" data-phone="${ item.userPhone }" data-account="${ item.userAccount }" data-flag="false" data-id="${ item.id }" data-orgtype="${ item.orgType }" data-orgmembertype="${ item.orgMemberType }" data-memberorgid="${ item.memberOrgId }" data-adminorgid="${ item.adminOrgId }" data-userid="${ item.userId }">
              <div class="node-children-name cursor"><span class="${ icon } node-event-parent"></span> <span class="user-select"></span> ${ item.shortName || item.memberName}</div>
            </div>
          `
        })
        el.append(dom)
        approve.dom.showRight()
      },
      //显示打勾
      showRight() {
        let data = approve.data.selectPerson;
        if (data.length) {
          $('.approve-node .user-select').removeClass('user-select-active')
          data.map(item => {
            $('.approve-node .user-select').each((index0, item0) => {
              if (item.id == $(item0).parent().parent().attr('data-memberorgid')) {
                $(item0).addClass('user-select-active')
              }
            })
          })
        } else {
          $('.approve-node .user-select').removeClass('user-select-active')
        }
      }
    },
    event: {
      //钉钉微信导航切换
      ddwxTabEvent() {
        let preants = $('html');
        let fn = function(e) {
          if (e.target.className.indexOf('approve-tab') > -1) {
            let $this = $(e.target);
            $this.addClass('approve-tab-active').siblings().removeClass('approve-tab-active')
            approve.data.type = $this[0].dataset.orgtype;
            approve.data.pid = $this[0].dataset.pid;
            approve.data.initId = $this[0].dataset.id;
            approve.data.getCount = false;
            approve.data.getCount = false;
            approve.methods.detailList($this[0].dataset.id, $('.approve-bottom'), true)
          }
        }
        preants.off('click')
        preants.unbind('click', fn).on('click', fn)
      },
      //微信钉钉列表下拉
      wxddNodeEvent(fn) {
        let count = 0;
        let preants = $('body');
        let nodeFn = function(e) {

          // alert(55);
          if (e.target.className.indexOf('node-children-name') > -1) {
            let $this = $(e.target);
            let parentNode = $this.parent('.approve-node');
            let parentNodePid = parentNode.attr('data-id');

            if (isNaN(parentNode.attr('data-userid'))) {
              // alert(00);
              if (parentNode.attr('data-flag') == 'false') {
                approve.methods.detailList(parentNodePid, parentNode)
              }
              parentNode.attr('data-flag', 'true')
              if (parentNode.attr('data-show') == 'false') {
                parentNode.attr('data-show', 'true')
                $this.siblings('.approve-node').css('display', 'block')
                $this.find('.icon-Gm-prefix').addClass('icon-GMopen').removeClass('icon-Gm-prefix')
                return
              }
              if (parentNode.attr('data-show') == 'true') {
                parentNode.attr('data-show', 'false')
                $this.siblings('.approve-node').css('display', 'none')
                $this.find('.icon-GMopen').addClass('icon-Gm-prefix').removeClass('icon-GMopen')
              }
            }
            if (!isNaN(parentNode.attr('data-userid'))) {
              // alert(111);
              if (!parentNode.find('.user-select').hasClass('user-select-active')) {

                approve.data.selectPerson.push({
                  id: parentNode.attr('data-memberorgid'),
                  name: parentNode.find('.node-children-name').text().trim(),
                  phone: parentNode.attr('data-phone'),
                  account: parentNode.attr('data-account')
                })
                approve.dom.showRight()
                if (fn) fn(approve.data.selectPerson)
                return
              }
              if (parentNode.find('.user-select').hasClass('user-select-active')) {
                approve.data.selectPerson.map((item, index) => {
                  if (item.id == parentNode.attr('data-memberorgid')) {
                    approve.data.selectPerson.splice(index, 1)
                  }
                })
                approve.dom.showRight()
                if (fn) fn(approve.data.selectPerson)

              }
            }
            return
          }
          if (e.target.className.indexOf('user-select') > -1) {
            let $this = $(e.target);
            let parentNode = $this.parent().parent('.approve-node');
            let parentNodePid = parentNode.attr('data-id');

            if (!isNaN(parentNode.attr('data-memberorgid'))) {
              if (!$this.hasClass('user-select-active')) {

                approve.data.selectPerson.push({
                  id: parentNode.attr('data-memberorgid'),
                  name: $this.parent().text().trim(),
                  phone: parentNode.attr('data-phone'),
                  account: parentNode.attr('data-account')
                })
                approve.dom.showRight()
                if (fn) fn(approve.data.selectPerson)
                return
              }
              if ($this.hasClass('user-select-active')) {
                approve.data.selectPerson.map((item, index) => {
                  if (item.id == parentNode.attr('data-memberorgid')) {
                    approve.data.selectPerson.splice(index, 1)
                  }
                })
                approve.dom.showRight()
                if (fn) fn(approve.data.selectPerson)

              }
            }
          }
        }
        preants.off('click')
        preants.unbind('click', nodeFn).on('click', nodeFn)
      }
    }
  }
  approve.init()
}())
